<?php
declare (strict_types = 1);

namespace app\api\controller\v1;


use think\facade\Request;
use app\api\service\StatusCode;
use app\api\service\UserToken;
use app\api\validate\Student as ValidateStudent;
use app\api\model\StudentManagement as ModelStudentManagement;
use app\api\validate\ParamMustBePostiveInt;
/**
 * 教师端学生管理类
 */
class StudentManagement extends Api
{

    protected $autoWriteTimestamp = 'datetime';
    /**
     * 获取所有学员信息
     */
    public function index()
    {
        $page = (new ParamMustBePostiveInt('page'))->paramCheck();
        $size = (new ParamMustBePostiveInt('size'))->paramCheck();
        $data = ModelStudentManagement::getAllStatus($page,$size);
        exit(json_encode($data));
    }

    /**
     * 
     * 新增学员信息
     */

    public function create()
    {
        $data = Request::post();
        validate(ValidateStudent::class)->scene('createStudent')->check($data);
        $success = ModelStudentManagement::addStudent($data);
        exit(json_encode($success));
    }

    /**
     * 
     * 搜索学员信息
     */

    public function search()
    {
        $data = Request::post();
        
        validate(ValidateStudent::class)->scene('search')->check($data);
        if(empty($data['name']) && empty($data['province_code']) && empty($data['city_code'])){
            exit(json_encode([
                'code' => 400,
                'message' => '搜索值不能为空'
            ]));
        }
        if(! empty($data['name'])){
            validate(ValidateStudent::class)->scene('checkName')->check($data);
        }

        if(! empty($data['province_code'])){
            validate(ValidateStudent::class)->scene('checkProvince_code')->check($data);
        }
        if(! empty($data['city_code'])){
            validate(ValidateStudent::class)->scene('checkCity_code')->check($data);
        }
        $data = ModelStudentManagement::searchStudent($data);
        exit(json_encode($data));
    }

    /**
     * 
     * 编辑学员信息
     */

    public function update()
    {
        $data = Request::post();
        validate(ValidateStudent::class)->scene('updateStudent')->check($data);
        $success = ModelStudentManagement::updateStudent($data);
        exit(json_encode($success));
    }

    /**
     * 
     * 删除学员信息
     */

    public function delete()
    {
        $id = (new ParamMustBePostiveInt('id'))->paramCheck();
        $success = ModelStudentManagement::delStudent($id);
        exit(json_encode($success));
    }

    /**
     * 
     * 获取学员详细信息
     */

    public function read()
    {
        $id = (new ParamMustBePostiveInt('id'))->paramCheck();
        $data = ModelStudentManagement::getStudentInfo($id);
        exit(json_encode($data));
    }
    
}